﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ExtAspNet;
using System.Data;

public partial class Module_CustServerManger_CustServerDetail : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BusinessStaff.OnClientTriggerClick = Window2.GetSaveStateReference(BusinessStaffID.ClientID, TelPhoneBusiness.ClientID, BusinessStaff.ClientID) + Window2.GetShowReference("../StaffManger/StaffTree.aspx", "选择业务人员");
        ServerStaff.OnClientTriggerClick = Window2.GetSaveStateReference(ServerStaffID.ClientID, TelPhoneServer.ClientID, ServerStaff.ClientID) + Window2.GetShowReference("../StaffManger/StaffTree.aspx", "选择客户服务人员");
       

        if (!IsPostBack)
        {
            if (Request.QueryString["CustID"] != null)
            {
                LoadCustData(Request.QueryString["CustID"].ToString());
            } 
        }
    }


    protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
    {

        System.Web.UI.WebControls.LinkButton lbf = (System.Web.UI.WebControls.LinkButton)Grid1.Rows[e.RowIndex].FindControl("lbfAction1");

        string str = Grid1.DataKeys[e.RowIndex][0].ToString();

        lbf.OnClientClick = "AddTab('Module/CustServerManger/MyServerCustDetailed.aspx?CustID=" + str + "', 'MyCustServer_MyCustServerDetailed', '客户服务详细信息')";
    }



    private void LoadCustData(string CustID)
    {
        Ylh.BLL.CustBLL bllc = new Ylh.BLL.CustBLL(DbConnectionStr);

        Ylh.Model.TbCust model = bllc.GetModel(Convert.ToInt32(CustID));

        this.CustID.Text = model.CustID.ToString();
        this.ServerStaffID.Text = model.ServerStaffID.ToString();
        this.BusinessStaffID.Text = model.BusinessStaffID.ToString();
        this.CustName.Text = model.CustName;
        this.CompanyName.Text = model.CompanyName;
        this.TelPhone.Text = model.TelPhone;
        this.Industry.Text = model.Industry;
        this.CustAddress.Text = model.CustAddress;
        this.PayCount.Text = model.PayCount.ToString();
        this.No.Text = model.No;
        this.OrdersCode.Text = model.OrdersCode;
        this.ID.Text = model.ID;
        this.Pwd.Text = model.Pwd;
        this.QQ.Text = model.QQ;
        this.mail.Text = model.mail;
        this.Website.Text = model.Website;
        this.ClearTime.SelectedDate = Convert.ToDateTime(model.ClearTime);

        if (!string.IsNullOrEmpty(BusinessStaffID.Text))
        {
            LoadBussInfo(BusinessStaffID.Text);
        }

        if (!string.IsNullOrEmpty(ServerStaffID.Text))
        {
            LoadServerInfo(ServerStaffID.Text);
        }


        if (string .IsNullOrEmpty(this.CustID.Text))
        {
            LoadServerList(this.CustID.Text);
        }



    }

    private void LoadServerList(string CustID)
    {
        Ylh.BLL.CustServerBLL bllc = new Ylh.BLL.CustServerBLL(DbConnectionStr);
        Grid1.DataSource = bllc.SelectServerInfo(Convert.ToInt32(CustID));

        Grid1.DataBind();
    }

   

   

    private void LoadServerInfo(string ServerStaffID)
    {
        Ylh.BLL.StaffBLL bll = new Ylh.BLL.StaffBLL(DbConnectionStr);
        DataTable dt = bll.GetStaff(Convert.ToInt32(ServerStaffID));

        ServerStaff.Text = dt.Rows[0]["StaffName"].ToString();
        TelPhoneServer.Text = dt.Rows[0]["Tel"].ToString();
    }

    private void LoadBussInfo(string BusinessStaffID)
    {
        Ylh.BLL.StaffBLL bll = new Ylh.BLL.StaffBLL(DbConnectionStr);
        DataTable dt = bll.GetStaff(Convert.ToInt32(BusinessStaffID));

        BusinessStaff.Text = dt.Rows[0]["StaffName"].ToString();
        TelPhoneBusiness.Text = dt.Rows[0]["Tel"].ToString();
    }


    private Ylh.Model.TbCust GetModel()
    {
        int BusinessStaffID = int.Parse(this.BusinessStaffID.Text);
        bool IsProtect = false;
        bool IsTransfer = true;
        DateTime Transfer = DateTime.Now;
        DateTime Creatime = DateTime.Now;

        string CustName = this.CustName.Text;
        string CompanyName = this.CompanyName.Text;
        string TelPhone = this.TelPhone.Text;
        string Industry = this.Industry.Text;
        string CustAddress = this.CustAddress.Text;
        decimal PayCount = decimal.Parse(this.PayCount.Text);
        string No = this.No.Text;
        string OrdersCode = this.OrdersCode.Text;
        string ID = this.ID.Text;
        string Pwd = this.Pwd.Text;
        string QQ = this.QQ.Text;
        string mail = this.mail.Text;
        string Website = this.Website.Text;
        DateTime ClearTime = DateTime.Parse(this.ClearTime.Text);
        Ylh.Model.TbCust model = new Ylh.Model.TbCust();

        if (!string.IsNullOrEmpty(this.ServerStaffID.Text))
        {
            model.ServerStaffID = Convert.ToInt32(ServerStaffID.Text);
            model.ServerTime = DateTime.Now;
        }
        else
        {
            model.ServerStaffID = null;
            model.ServerTime = null;
        }


        if (!string.IsNullOrEmpty(CustID.Text))
        {
            model.CustID = Convert.ToInt32(CustID.Text);
        }
        model.BusinessStaffID = BusinessStaffID;
        model.IsProtect = IsProtect;
        model.IsTransfer = IsTransfer;
        model.Transfer = Transfer;
        model.Creatime = Creatime;

        model.CustName = CustName;
        model.CompanyName = CompanyName;
        model.TelPhone = TelPhone;
        model.Industry = Industry;
        model.CustAddress = CustAddress;
        model.PayCount = PayCount;
        model.No = No;
        model.OrdersCode = OrdersCode;
        model.ID = ID;
        model.Pwd = Pwd;
        model.QQ = QQ;
        model.mail = mail;
        model.Website = Website;
        model.ClearTime = ClearTime;
        model.Linkman = "";
        model.CompanyAddress = "";
        model.From = "";
        model.RegisterTime = null;
        model.Character = "";
        model.Hobby = "";
        model.Expect = "";
        model.Network = "";
        model.advantage = "";
        model.KeyServer = "";
        model.Legal = "";
        model.IsDelet = false;


        return model;

    }




    protected void BtnSave_Click(object sender, EventArgs e)
    {

        if (string.IsNullOrEmpty(PayCount.Text))
        {

            Alert.Show("金额不能为空");
            return;
        }

        if (string.IsNullOrEmpty(ClearTime.Text))
        {
            Alert.Show("开通时间不能为空");
            return;
        }

        if (string.IsNullOrEmpty(BusinessStaffID.Text))
        {
            Alert.Show("业务员不能为空");
            return;
        }


        Ylh.BLL.CustBLL bll = new Ylh.BLL.CustBLL(DbConnectionStr);

        Ylh.Model.TbCust cust = GetModel();

        if (string.IsNullOrEmpty(CustID.Text))
        {
            CustID.Text = bll.InsertCustInfo(cust).ToString();
            //插入操作
            Alert.Show("信息插入成功");
            return;
        }
        else
        {
            //修改操作
            bll.UpdateCustInfo(cust);
            Alert.Show("信息更新成功");
            return;
        }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(CustID.Text))
        {
            PageContext.RegisterStartupScript(Window3.GetShowReference("MyServerCustInfo.aspx?CustID=" + CustID.Text, "添加服务信息"));
        }
        else
        {
            Alert.Show("请先保存客户信息");
        }
        
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        List<string> li = base.GetCheckID(Grid1);

        if (li.Count==0)
        {
            Alert.Show("当前没有选中任何记录");
            return;
        }

        Ylh.BLL.CustServerBLL blls = new Ylh.BLL.CustServerBLL(DbConnectionStr);

        blls.DeleteServerInfo(li);

        Alert.Show("删除成功");
    }
}